﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/4/7 星期日 21:34:09
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.DataDictionary.CORS;

namespace Sector7.Framework.DataDictionary.Agents
{
    /// <summary>
    /// 数据元服务代理
    /// </summary>
    public class DataElementServiceAgent
    {
        /// <summary>
        /// 添加数据元
        /// </summary>
        /// <param name="element">数据元</param>
        /// <returns>操作结果</returns>
        public static int Add(S7_DataElement element)
        {
            object result = WcfClient.GetInstance().Invoke<object>(
                service => service.AddDataElement(element));

            return Convert.ToInt32(result);
        }

        /// <summary>
        /// 根据数据分组获取所有数据元
        /// </summary>
        /// <param name="groupId">数据分组编号</param>
        /// <returns>数据元列表</returns>
        public static List<S7_DataElement> GetAll(int groupId)
        {
            List<S7_DataElement> elements = null;

            try
            {
                elements = WcfClient.GetInstance().Invoke<List<S7_DataElement>>(
                    service => service.GetAllDataElementsByGroupId(groupId));
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.ERR, "DAD", "GetAllDataElementsByGroupId 读取数据元列表异常！");
                Log.WriteLine(LogSeverityType.ERR, "DAD", e);
            }

            return elements;
        }

        /// <summary>
        /// 更新数据元
        /// </summary>
        /// <param name="delta">数据元改变量</param>
        /// <returns>操作结果</returns>
        public static int Set(EntityDeltaBase delta)
        {
            return WcfClient.GetInstance().Invoke<int>(
                service => service.SetDataElement(delta));
        }

        /// <summary>
        /// 删除数据元
        /// </summary>
        /// <param name="elementId">数据元编号</param>
        /// <returns>操作结果</returns>
        public static int Delete(string elementId)
        {
            return WcfClient.GetInstance().Invoke<int>(
                service => service.DeleteDataElement(elementId));
        }
    }
}
